library(dplyr)
library(ggplot2)

source('../helper_functions/s4_temporal_plot.R')
library(lubridate)
library(ggnewscale)
library(wesanderson)
daynamica_data <- params$daynamica_data
if(is.null(params$color_codings)){


  unique_activity_labels <- 
    daynamica_data$ucalitems_temporal_plot %>%
    tibble() %>%
    filter(type_decoded == "ACTIVITY") %>%
    pull(subtype_decoded) %>% unique()
  unique_trip_labels <- 
    daynamica_data$ucalitems_temporal_plot %>%
    tibble() %>%
    filter(type_decoded == "TRIP") %>%
    pull(subtype_decoded) %>% unique()
  
activity_palette <- c(wes_palette("Zissou1", 1),
                      wes_palette("Zissou1", 5)[4:5],
                      rev(wes_palette("Royal1", 4)[2:4]),
                      wes_palette("Moonrise3", 2),
                      wes_palette("GrandBudapest2"),
                      wes_palette("GrandBudapest1"),
                      wes_palette("Chevalier1", 4))[1:length(unique_activity_labels)]
  
  
  trip_palette <- c(palette.colors(n = 8, "ggplot2")[-1],
                    palette.colors(n = 8, "alphabet"))[1:length(unique_trip_labels)]
  
  
  color_codes_activity_data <- data.frame(labels = unique_activity_labels,
                                     values = activity_palette)
  
  color_codes_trips_data <- data.frame(labels = unique_trip_labels,
                                  values = trip_palette)
}else{
  color_codes_activity_data <- params$color_codings$color_codes_activity_data
  color_codes_trips_data <- params$color_codings$color_codes_trips_data
}

if(is.null(params$limit_tod)){
  min_time = 0
  max_time = 1
} else{
  min_time <- limit_tod[1]
  max_time <- limit_tod[2]
}

Visualization Notes

These plots show 7-day views of the calendar item data collected via the Daynamica app. Each figure starts on a Sunday. For participants who collected data for longer than one week, there will be multiple plots which can be accessed via separate tabs.

Colors and Category levels can be modified via dataset manipulations and report parameters. See the master_analysis_file.R for further details.

User Calendar Item Visualizations

pages <- ceiling(length(user_id_list) /10)
user_id_list_short <- sub("@[^@]*$", "", user_id_list)
for(i in 1:pages){
  if(i != pages){
    id_index <- (1 + 10*(i-1)):(10 + 10*(i-1))
  } else{
    id_index <-(1 + 10*(i-1)):length(user_id_list)
  }
  
  cat("\n\n## Participants: ", min(id_index), "-", max(id_index), " {.tabset} \n")
  for(j in id_index){
    cat("\n\n### ", user_id_list_short[j], " {.tabset} \n")
      #print(user_id_list_short[j])
      
      temp_data <- daynamica_data$ucalitems_temporal_plot %>%
        as_tibble() %>%
        filter(user_id == user_id_list[j])
      
      tab <- tibble(start_date = as.Date(min(temp_data$start_date):max(temp_data$start_date), origin = "1970-01-01"),
             day_number = lubridate::wday(start_date),
             check = ifelse(day_number == 1, 1, 0),
             cum_total = cumsum(check))
      
      
      # tab <- temp_data %>%
      #   select(start_date, dow) %>%
      #   mutate(day_number = lubridate::wday(start_date)) %>%
      #   distinct() %>%
      #   arrange(start_date) %>%
      #   mutate(check = ifelse(day_number == 1, 1, 0),
      #          cum_total = cumsum(check))
      
      unique_weeks <- unique(tab$cum_total)
      for(k in 1:length(unique_weeks)){
        current_dates <- tab %>%
          filter(cum_total == unique_weeks[k])
        suppressWarnings(dates_this_week <- as.Date((current_dates$start_date - current_dates$day_number + 1) + 0:6, origin = "1970-01-01"))
        
        #tab_text <- paste0(min(current_dates$start_date), " : ", max(current_dates$start_date))
        tab_text <- paste0(min(dates_this_week), " : ", max(dates_this_week))
        cat("\n\n#### ", tab_text, " \n")
        analysis_data <- temp_data %>%
          filter(start_date %in% dates_this_week)
        if(nrow(analysis_data) == 0){
          print("No data during this date range")
          next
        }
          analysis_data <- analysis_data %>%
            bind_rows(analysis_data[rep(1, 7),] %>% # This code allows all days of the week to be displayed even if we don't have data on those dates.
                      mutate(start_time = "04:00:00",
                             end_time = "04:00:01",
                             start_date = dates_this_week,
                             type_decoded = "ACTIVITY")) %>%
            mutate(end_time = ifelse(end_time == "00:00:00", "24:00:00", end_time))
      p <- suppressMessages(generate_time_of_day_plot(analysis_data,
                           color_codes_activity = color_codes_activity_data,
                           color_codes_trips = color_codes_trips_data,
                           min_time = min_time,
                           max_time = max_time)) + 
        ggtitle(paste0("Calendar Item Visualization: ", user_id_list_short[j]),
                subtitle = paste0("Date Range: ", tab_text))
      print(p)
      }
  }
}

Participants: 1 - 10

user_id_122

2022-11-13 : 2022-11-19

2022-11-20 : 2022-11-26

2022-11-27 : 2022-12-03

2022-12-04 : 2022-12-10

2022-12-11 : 2022-12-17

2022-12-18 : 2022-12-24

[1] “No data during this date range”

2022-12-25 : 2022-12-31

2023-01-01 : 2023-01-07

user_id_125

2022-12-04 : 2022-12-10

2022-12-11 : 2022-12-17

user_id_13

2022-12-04 : 2022-12-10

2022-12-11 : 2022-12-17

user_id_142

2023-01-01 : 2023-01-07

2023-01-08 : 2023-01-14

user_id_148

2022-12-11 : 2022-12-17

user_id_159

2022-11-13 : 2022-11-19

2022-11-20 : 2022-11-26

2022-11-27 : 2022-12-03

2022-12-04 : 2022-12-10

user_id_165

2023-02-19 : 2023-02-25

2023-02-26 : 2023-03-04

2023-03-05 : 2023-03-11

user_id_17

2022-11-06 : 2022-11-12

2022-11-13 : 2022-11-19

2022-11-20 : 2022-11-26

user_id_192

2023-01-22 : 2023-01-28

user_id_219

2023-01-15 : 2023-01-21

2023-01-22 : 2023-01-28

Participants: 11 - 15

user_id_233

2022-11-13 : 2022-11-19

2022-11-20 : 2022-11-26

user_id_24

2022-12-18 : 2022-12-24

2022-12-25 : 2022-12-31

2023-01-01 : 2023-01-07

user_id_241

2023-01-15 : 2023-01-21

2023-01-22 : 2023-01-28

2023-01-29 : 2023-02-04

user_id_271

2022-10-23 : 2022-10-29

user_id_277

2023-01-15 : 2023-01-21